package com.dyp.filterdemo.web.filter;

import org.springframework.stereotype.Component;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpServletResponseWrapper;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;

//配置拦截路径
//@Component //如果没有这个注解需要把FilterDemoApplication下的ServletComponentScan注解打开
@WebFilter(filterName = "SpringWebFilter",urlPatterns = {"/test/*"})
public class SpringWebFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        Filter.super.init(filterConfig);
    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        HttpServletResponse response = (HttpServletResponse) servletResponse;
        System.out.println("SpringWebFilter,"+request.getRequestURI());
        ModifyParamRequestWrapper wrappedRequest = new ModifyParamRequestWrapper((HttpServletRequest) servletRequest);
        wrappedRequest.addParameter("timestamp",  "202504271029"); // 添加当前时间参数
        wrappedRequest.addParameter("source",  "filter_modify");   // 添加来源标记

        // 包装原始的Response对象
        CharResponseWrapper wrappedResponse = new CharResponseWrapper(response);

        filterChain.doFilter(wrappedRequest, wrappedResponse);

        // 获取原始响应内容
        String originalResponse = wrappedResponse.getResponseBody();

        // 在原始响应后添加一行数据
        String modifiedResponse = originalResponse + "\nAdded by Spring Web Filter";

        // 将修改后的内容写回原始Response
        response.setContentLength(modifiedResponse.length());  // 更新Content-Length
        response.getWriter().write(modifiedResponse);

    }

    @Override
    public void destroy() {
        Filter.super.destroy();
    }

    // 自定义Response包装类
    private static class CharResponseWrapper extends HttpServletResponseWrapper {
        private final ByteArrayOutputStream bos;
        private final ServletOutputStream out;
        private final PrintWriter writer;

        public CharResponseWrapper(HttpServletResponse response) throws IOException {
            super(response);
            this.bos = new ByteArrayOutputStream();
            this.out = new ServletOutputStream() {
                @Override
                public boolean isReady() {
                    return true;
                }

                @Override
                public void setWriteListener(WriteListener writeListener) {

                }

                @Override
                public void write(int b) throws IOException {
                    bos.write(b);
                }

                @Override
                public void write(byte[] b) throws IOException {
                    bos.write(b);
                }
            };
            this.writer = new PrintWriter(out);
        }

        @Override
        public ServletOutputStream getOutputStream() throws IOException {
            return this.out;
        }

        @Override
        public PrintWriter getWriter()  {
            return this.writer;
        }
//        public PrintWriter getWriter()  {
//            return new PrintWriter(out);
//        }


        public String getResponseBody() throws IOException {
//            ServletOutputStream outputStream = this.getOutputStream();
//            outputStream.flush();

            PrintWriter writer = this.getWriter();
            writer.flush();  //把PrintWriter中缓存的数据刷新到PrintWriter.out中，最后实际是写到了bos中。

//            return bos.toString("UTF-8".toString());
            return bos.toString(this.getCharacterEncoding());

        }
    }
}
